package com.dawn.module.hospital.service.msgsenduser;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dawn.framework.common.enums.CommonStatusEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.dawn.module.hospital.controller.admin.msgsenduser.vo.*;
import com.dawn.module.hospital.dal.dataobject.msgsenduser.MsgSendUserDO;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.util.object.BeanUtils;

import com.dawn.module.hospital.dal.mysql.msgsenduser.MsgSendUserMapper;

import static com.dawn.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.dawn.module.hospital.enums.ErrorCodeConstants.*;

/**
 * 消息推送用户 Service 实现类
 *
 * @author root
 */
@Service
@Validated
public class MsgSendUserServiceImpl implements MsgSendUserService {

    @Resource
    private MsgSendUserMapper msgSendUserMapper;

    @Override
    public Long createMsgSendUser(MsgSendUserSaveReqVO createReqVO) {
        // 插入
        MsgSendUserDO msgSendUser = BeanUtils.toBean(createReqVO, MsgSendUserDO.class);
        msgSendUserMapper.insert(msgSendUser);
        // 返回
        return msgSendUser.getId();
    }

    @Override
    public void updateMsgSendUser(MsgSendUserSaveReqVO updateReqVO) {
        // 校验存在
        validateMsgSendUserExists(updateReqVO.getId());
        // 更新
        MsgSendUserDO updateObj = BeanUtils.toBean(updateReqVO, MsgSendUserDO.class);
        msgSendUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteMsgSendUser(Long id) {
        // 校验存在
        validateMsgSendUserExists(id);
        // 删除
        msgSendUserMapper.deleteById(id);
    }

    private void validateMsgSendUserExists(Long id) {
        if (msgSendUserMapper.selectById(id) == null) {
            throw exception(MSG_SEND_USER_NOT_EXISTS);
        }
    }

    @Override
    public MsgSendUserDO getMsgSendUser(Long id) {
        return msgSendUserMapper.selectById(id);
    }

    @Override
    public List<MsgSendUserDO> getListByType(String sendType) {
        return msgSendUserMapper.selectList(new LambdaQueryWrapper<MsgSendUserDO>()
                .eq(MsgSendUserDO::getSendType, sendType)
                .eq(MsgSendUserDO::getStatus, CommonStatusEnum.ENABLE.getStatus()));
    }

    @Override
    public PageResult<MsgSendUserDO> getMsgSendUserPage(MsgSendUserPageReqVO pageReqVO) {
        return msgSendUserMapper.selectPage(pageReqVO);
    }

}